<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检验单编号" prop="iqcCode" label-width="100px">
        <el-input
          v-model="queryParams.iqcCode"
          placeholder="请输入检验单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['qc:inspection:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['qc:inspection:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['qc:inspection:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['qc:inspection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="iqcId" />-->
      <el-table-column label="检验单编号" align="center" prop="iqcCode" />
<!--      <el-table-column label="物料ID" align="center" prop="materialId" />-->
      <el-table-column label="物料名称" align="center" prop="materialName" />
<!--      <el-table-column label="物料规格" align="center" prop="spec" />-->
<!--      <el-table-column label="检验项目" align="center" prop="inspectionItem" />-->
<!--      <el-table-column label="检验人ID" align="center" prop="inspectionerId" />-->
      <el-table-column label="订单号" align="center" prop="orderBillno" />
      <el-table-column label="订单数量" align="center" prop="actReceiveQty" />
<!--      <el-table-column label="需求数量" align="center" prop="needQty" />-->
      <el-table-column label="实收数量" align="center" prop="actlandQty" />
      <el-table-column label="合格数量" align="center" prop="receiveQty" />
      <el-table-column label="判退数量" align="center" prop="refuseQty" />
      <el-table-column label="收料通知单单据编号" align="center" prop="billNo" />
<!--      <el-table-column label="供应商ID" align="center" prop="supplierId" />-->
<!--      <el-table-column label="仓库ID" align="center" prop="stocklocId" />-->
<!--      <el-table-column label="状态" align="center" prop="status" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="单据类型" align="center" prop="fbillTypeID" />
      <el-table-column label="业务类型" align="center" prop="fbusinessType" />
      <el-table-column label="检验结果" align="center" prop="finspectResult" />
<!--      <el-table-column label="质检状态" align="center" prop="fqcstatus" />-->
<!--      <el-table-column label="来源组织" align="center" prop="fsourceOrgId" />-->
<!--      <el-table-column label="质检组织" align="center" prop="finspectOrgId" />-->
      <el-table-column label="单据日期" align="center" prop="fdate" />
<!--      <el-table-column label="时间单位" align="center" prop="ftimeUnit" />-->
<!--      <el-table-column label="样本破坏承担方" align="center" prop="fsampleDamageBearer" />-->
<!--      <el-table-column label="当前严格度" align="center" prop="fcurrentStringency" />-->
<!--      <el-table-column label="状态1" align="center" prop="fpolicyStatus" />-->
      <el-table-column label="使用决策" align="center" prop="fusePolicy" />
<!--      <el-table-column label="入库关联标志" align="center" prop="finstockFlag" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBack(scope.row)"
            v-hasPermi="['qc:inspection:edit']"
          >回写ERP</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleAddpurchasewarehousing(scope.row)"
            v-hasPermi="['qc:inspection:remove']"
          >生成采购入库单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改IQC检验单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检验单编号" prop="iqcCode">
          <el-input v-model="form.iqcCode" placeholder="请输入检验单编号" />
        </el-form-item>
        <el-form-item label="物料ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料ID" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料规格" prop="spec">
          <el-input v-model="form.spec" placeholder="请输入物料规格" />
        </el-form-item>
        <el-form-item label="检验项目" prop="inspectionItem">
          <el-input v-model="form.inspectionItem" placeholder="请输入检验项目" />
        </el-form-item>
        <el-form-item label="检验人ID" prop="inspectionerId">
          <el-input v-model="form.inspectionerId" placeholder="请输入检验人ID" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderBillno">
          <el-input v-model="form.orderBillno" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单数量" prop="actReceiveQty">
          <el-input v-model="form.actReceiveQty" placeholder="请输入订单数量" />
        </el-form-item>
        <el-form-item label="需求数量" prop="needQty">
          <el-input v-model="form.needQty" placeholder="请输入需求数量" />
        </el-form-item>
        <el-form-item label="实收数量" prop="actlandQty">
          <el-input v-model="form.actlandQty" placeholder="请输入实收数量" />
        </el-form-item>
        <el-form-item label="合格数量" prop="receiveQty">
          <el-input v-model="form.receiveQty" placeholder="请输入合格数量" />
        </el-form-item>
        <el-form-item label="判退数量" prop="refuseQty">
          <el-input v-model="form.refuseQty" placeholder="请输入判退数量" />
        </el-form-item>
        <el-form-item label="收料通知单单据编号" prop="billNo">
          <el-input v-model="form.billNo" placeholder="请输入收料通知单单据编号" />
        </el-form-item>
        <el-form-item label="供应商ID" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商ID" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="stocklocId">
          <el-input v-model="form.stocklocId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspection, getInspection, delInspection, IQCDataBack, updateInspection } from "@/api/mes/qc/iqcinspection";
import {addWarehousing} from "@/api/mes/wm/purchasewarehousing";

export default {
  name: "IqcInspection",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // IQC检验单表格数据
      inspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        iqcCode: null,
        materialName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询IQC检验单列表 */
    getList() {
      this.loading = true;
      listInspection(this.queryParams).then(response => {
        this.inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iqcId: null,
        iqcCode: null,
        materialId: null,
        materialName: null,
        spec: null,
        inspectionItem: null,
        inspectionerId: null,
        orderBillno: null,
        actReceiveQty: null,
        needQty: null,
        actlandQty: null,
        receiveQty: null,
        refuseQty: null,
        billNo: null,
        supplierId: null,
        stocklocId: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.pageNum = 1;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.iqcId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加IQC检验单";
    },
    handleBack(row){
      const iqcId = row.iqcId || this.ids
      getInspection(iqcId).then(response => {
        this.form = response.data;
        console.info('this.form===>',this.form)
        IQCDataBack(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });

    },

    handleAddpurchasewarehousing(row){
      const iqcId = row.iqcId || this.ids
      getInspection(iqcId).then(response => {
        this.form = response.data;
        addWarehousing(this.form).then(response =>{
          this.$modal.msgSuccess("生成采购入库单成功");
          this.getList();
        });
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iqcId = row.iqcId || this.ids
      getInspection(iqcId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改IQC检验单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.iqcId != null) {
            updateInspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const iqcIds = row.iqcId || this.ids;
      this.$modal.confirm('是否确认删除IQC检验单编号为"' + iqcIds + '"的数据项？').then(function() {
        return delInspection(iqcIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qc/inspection/export', {
        ...this.queryParams
      }, `inspection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
